package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Acceso;
import com.lostreseditores.sabersai.model.dto.AccesoDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class AccesoView {
    private InputText txtMenuidn_Menu;
    private InputText txtNiveidn_Nivel;
    private InputText txtAcceidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<AccesoDTO> data;
    private AccesoDTO selectedAcceso;

    public AccesoView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            AccesoDTO accesoDTO = (AccesoDTO) e.getObject();

            if (txtMenuidn_Menu == null) {
                txtMenuidn_Menu = new InputText();
            }

            txtMenuidn_Menu.setValue(accesoDTO.getMenuidn_Menu());

            if (txtNiveidn_Nivel == null) {
                txtNiveidn_Nivel = new InputText();
            }

            txtNiveidn_Nivel.setValue(accesoDTO.getNiveidn_Nivel());

            if (txtAcceidn == null) {
                txtAcceidn = new InputText();
            }

            txtAcceidn.setValue(accesoDTO.getAcceidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtMenuidn_Menu != null) {
            txtMenuidn_Menu.setValue(null);
            txtMenuidn_Menu.setDisabled(true);
        }

        if (txtNiveidn_Nivel != null) {
            txtNiveidn_Nivel.setValue(null);
            txtNiveidn_Nivel.setDisabled(true);
        }

        if (txtAcceidn != null) {
            txtAcceidn.setValue(null);
            txtAcceidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Acceso entity = null;

        try {
            Integer acceidn = new Integer(txtAcceidn.getValue().toString());
            entity = BusinessDelegatorView.getAcceso(acceidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtMenuidn_Menu.setDisabled(false);
            txtNiveidn_Nivel.setDisabled(false);
            txtAcceidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtMenuidn_Menu.setValue(entity.getMenu().getMenuidn());
            txtMenuidn_Menu.setDisabled(false);
            txtNiveidn_Nivel.setValue(entity.getNivel().getNiveidn());
            txtNiveidn_Nivel.setDisabled(false);
            txtAcceidn.setValue(entity.getAcceidn());
            txtAcceidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveAcceso(FacesUtils.checkInteger(txtAcceidn),
                FacesUtils.checkInteger(txtMenuidn_Menu),
                FacesUtils.checkInteger(txtNiveidn_Nivel));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteAcceso(FacesUtils.checkInteger(
                    txtAcceidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateAcceso(FacesUtils.checkInteger(
                    txtAcceidn), FacesUtils.checkInteger(txtMenuidn_Menu),
                FacesUtils.checkInteger(txtNiveidn_Nivel));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtAcceidn == null) {
                txtAcceidn = new InputText();
            }

            txtAcceidn.setValue(selectedAcceso.getAcceidn());

            BusinessDelegatorView.deleteAcceso(FacesUtils.checkInteger(
                    txtAcceidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedAcceso);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Integer acceidn, Integer menuidn_Menu,
        Integer niveidn_Nivel) throws Exception {
        try {
            BusinessDelegatorView.updateAcceso(acceidn, menuidn_Menu,
                niveidn_Nivel);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("AccesoView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtMenuidn_Menu() {
        return txtMenuidn_Menu;
    }

    public void setTxtMenuidn_Menu(InputText txtMenuidn_Menu) {
        this.txtMenuidn_Menu = txtMenuidn_Menu;
    }

    public InputText getTxtNiveidn_Nivel() {
        return txtNiveidn_Nivel;
    }

    public void setTxtNiveidn_Nivel(InputText txtNiveidn_Nivel) {
        this.txtNiveidn_Nivel = txtNiveidn_Nivel;
    }

    public InputText getTxtAcceidn() {
        return txtAcceidn;
    }

    public void setTxtAcceidn(InputText txtAcceidn) {
        this.txtAcceidn = txtAcceidn;
    }

    public List<AccesoDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataAcceso();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<AccesoDTO> accesoDTO) {
        this.data = accesoDTO;
    }

    public AccesoDTO getSelectedAcceso() {
        return selectedAcceso;
    }

    public void setSelectedAcceso(AccesoDTO acceso) {
        this.selectedAcceso = acceso;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
